package com.ruoyi.quotation.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quotation.domain.QuoEngineering;
import com.ruoyi.quotation.domain.QuoPricing;
import com.ruoyi.quotation.domain.QuoQuote;
import com.ruoyi.quotation.mapper.QuoEngineeringMapper;
import com.ruoyi.quotation.mapper.QuoPricingMapper;
import com.ruoyi.quotation.mapper.QuoQuoteMapper;
import com.ruoyi.quotation.service.IQuoManagerService;
import com.ruoyi.quotation.vo.QuoteFullInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 总经理审核服务实现
 */
@Service
public class QuoManagerServiceImpl implements IQuoManagerService {

    @Autowired
    private QuoQuoteMapper quoQuoteMapper;

    @Autowired
    private QuoEngineeringMapper quoEngineeringMapper;

    @Autowired
    private QuoPricingMapper quoPricingMapper;

    /**
     * 查询待总经理审核的报价单列表
     *
     * @param quoQuote 查询条件
     * @return 报价单列表
     */
    @Override
    public List<QuoQuote> selectManagerQuoteList(QuoQuote quoQuote) {
        // 设置默认状态为"总经理审核中"，如果已经设置了状态则使用设置的状态
        if (quoQuote.getStatus() == null || quoQuote.getStatus().isEmpty()) {
            quoQuote.setStatus("总经理审核中");
        }
        return quoQuoteMapper.selectQuoteList(quoQuote);
    }

    /**
     * 获取报价单完整信息（包括工程审核和成本核算信息）
     *
     * @param id 报价单ID
     * @return 报价单完整信息
     */
    @Override
    public QuoteFullInfoVO getQuoteFullInfo(Integer id) {
        QuoteFullInfoVO fullInfoVO = new QuoteFullInfoVO();
        
        // 获取报价单信息
        QuoQuote quoQuote = quoQuoteMapper.selectQuoteById(id);
        fullInfoVO.setQuoQuote(quoQuote);
        
        if (quoQuote != null) {
            // 获取工程审核信息
            QuoEngineering quoEngineering = quoEngineeringMapper.selectQuoEngineeringByOrderNo(quoQuote.getOrderNo());
            fullInfoVO.setQuoEngineering(quoEngineering);
            
            // 获取成本核算信息
            QuoPricing quoPricing = quoPricingMapper.selectQuoPricingByOrderNo(quoQuote.getOrderNo());
            fullInfoVO.setQuoPricing(quoPricing);
        }
        
        return fullInfoVO;
    }

    /**
     * 执行总经理审核
     *
     * @param quoQuote 报价单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int auditQuote(QuoQuote quoQuote) {
        // 1. 更新报价单状态为"报价审核完成"
        quoQuote.setStatus("报价审核完成");
        quoQuote.setUpdateBy(SecurityUtils.getUsername());
        
        // 2. 更新报价单
        return quoQuoteMapper.updateQuoteStatusByOrderNo(quoQuote);
    }

    /**
     * 驳回审核
     *
     * @param quoQuote 报价单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int rejectQuote(QuoQuote quoQuote) {
        // 1. 更新报价单状态为"财务审核中"（驳回到上一步）
        quoQuote.setStatus("财务审核中");
        quoQuote.setUpdateBy(SecurityUtils.getUsername());
        
        // 2. 更新报价单
        return quoQuoteMapper.updateQuoteStatusByOrderNo(quoQuote);
    }
} 